package com.hcbd.tdwt.base;



import com.hcbd.tdwt.util.DateUtils;
import com.hcbd.tdwt.util.FileUtil;

import com.hcbd.tdwt.ui.xingyu.vo.BoxContact;
import com.hcbd.tdwt.ui.xingyu.vo.UserMessageVO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.TreeMap;

//图片接收类
public class ReceiveImgManager {
    private String path;
    private String uniqueSerialNumber;
    public static String hiddenSign="00";//隐藏标志
    public static String showSign="01";//显示标志
    private static final HashMap<String, UserMessageVO>userMessageHashMap=new HashMap<>();
    private static final HashMap<String, ReceiveImgManager> receiveImgMap=new HashMap<>();
    private final TreeMap<Integer,byte[]> sortMap=new TreeMap<>();

    private static Timer timer;
    private static TimerTask task;
    //开启定时器，判断接收图片是否超时
    public static void startTime() {
        if(timer==null) {
            timer = new Timer();
            task = new TimerTask() {
                @Override
                public void run() {
                    computationTimeout();
                }
            };
            timer.schedule(task, 120000, 120000);
        }
    }

    //超过两份钟便修改接收状态
    public static void computationTimeout(){
         Set<String> keySet = userMessageHashMap.keySet();
         if(keySet.size()==0){
             return;
         }
         for (String s : keySet) {
             UserMessageVO userMessage=userMessageHashMap.get(s);
             if(DateUtils.timeReduce(userMessage.getSendTime(), DateUtils.getDateLongSerial())>3){
                 userMessage.setReceiverSend(hiddenSign);
                 userMessage.update();
                 userMessageHashMap.remove(s);
             }
         }
     }
    public static void  clear(){
        if(timer!=null){
            timer.cancel();
            timer=null;
        }
        if(task!=null){
            task.cancel();
            task=null;
        }
        Set<String> keySet = userMessageHashMap.keySet();
        for (String s : keySet) {
            UserMessageVO userMessage=userMessageHashMap.get(s);
            userMessage.setReceiverSend(hiddenSign);
            userMessage.update();
        }
        receiveImgMap.clear();
        userMessageHashMap.clear();

    }
    public static ReceiveImgManager getReceiveImg(Integer id, String uniqueSerialNumber, byte[]content){
        ReceiveImgManager receiveImgData=receiveImgMap.get(uniqueSerialNumber);
        String imgFilePath= FileUtil.getReceiveImgFile()+"A9"+ DateUtils.getDateShortSerial()+".jpg";
        if(receiveImgData==null){
            receiveImgData=new ReceiveImgManager();
            receiveImgData.setUniqueSerialNumber(uniqueSerialNumber);
            receiveImgData.setPath(imgFilePath);
            receiveImgData.sortMap.put(id,content);
            receiveImgMap.put(uniqueSerialNumber,receiveImgData);
        }else{
            receiveImgData.setPath(imgFilePath);
            receiveImgData.sortMap.put(id,content);
        }
        return receiveImgData;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getUniqueSerialNumber() {
        return uniqueSerialNumber;
    }

    public void setUniqueSerialNumber(String uniqueSerialNumber) {
        this.uniqueSerialNumber = uniqueSerialNumber;
    }
    public List<byte[]> getListByte(){
        Set<Integer> keySet = sortMap.keySet();
        List<byte[]> list=new ArrayList<>();
        for (Integer s : keySet) {
            list.add(sortMap.get(s));
        }
        return list;
    }
    public static UserMessageVO getUserMsg(ReceiveImgManager receiveImgData, String fromCardNumber, int total, int id, long time){
        UserMessageVO userMessage =userMessageHashMap.get(receiveImgData.getUniqueSerialNumber());
        String uniqueSerialNumber=receiveImgData.getUniqueSerialNumber();
        if(userMessage==null){
            //返回消息
            userMessage = new UserMessageVO();
            userMessage.setReceiverNumber(fromCardNumber);
            userMessage.setSendNumber(BeidouBoxParams.userCardNumber);
            userMessage.setMsgType(Constant.PICTURE);
            userMessage.setPicSize(String.valueOf(total));
            userMessage.setReceivedPackets(String.valueOf(id+1));
            userMessage.setContent(receiveImgData.getPath());
            userMessage.setSendTime(DateUtils.timeStampToDate(String.valueOf(time), null));
            userMessage.setIoType(Constant.RECEIVE_MESSAGE);
            userMessage.setRead(false);
            userMessage.setUniqueSerial(uniqueSerialNumber);
            userMessage.setReceiverSend(showSign);
            //保存消息列表，未读
            BoxContact.cacheFromBoxId(fromCardNumber,Constant.PICTURE,"图片",true);
            //保存用户消息
            userMessage.insert();
            //叮咚
            BdManager.getInstance().updateRecentChatTable(userMessage.getContent());
            userMessageHashMap.put(uniqueSerialNumber,userMessage);
        }else{
            if((id+1)==total){
                userMessage.setReceiverSend(hiddenSign);
            }
            userMessage.setPicSize(String.valueOf(total));
            userMessage.setReceivedPackets(String.valueOf(id+1));
            userMessage.setContent(receiveImgData.getPath());
            userMessage.update();
        }
        return userMessage;
    }


}
